package com.tjtt.tdm.knowledge.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.tjtt.gwm.service.EquipmentService;
import com.tjtt.tdm.base.BaseAction;
import com.tjtt.tdm.base.PageResult;
import com.tjtt.tdm.base.PubMethod;
import com.tjtt.tdm.base.StringUtil;
import com.tjtt.tdm.exception.ServiceException;
import com.tjtt.tdm.knowledge.bean.KnowFileType;
import com.tjtt.tdm.knowledge.bean.KnowledgeHandOut;
import com.tjtt.tdm.knowledge.bean.KnowledgeManage;
import com.tjtt.tdm.knowledge.bean.KnowledgeType;
import com.tjtt.tdm.knowledge.service.IKnowledgeService;
import com.tjtt.tdm.system.service.LogService;


/**
 * 
 * @author songxiaoyan
 *
 */
public class KnowledgeAction extends BaseAction {
	
	private static final long serialVersionUID = 5156288255337069381L;
	private LogService logService;
	private IKnowledgeService knowledgeService;
	private EquipmentService epService;
	
	private String treeId;
	private int pid;
	private String operator;
	private KnowledgeManage knowledgeManage;
	
	private List upload; 
	private List uploadContentType; 
	private List uploadFileName; 

	private String page;
	private String rows;
	private String sidx;
	private String sord;
	private String fileType;
	private String filePath;
	private Long id;
	public String getFilePath() {
		return filePath;
	}
	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}
	/**
	 * 功能描述：文件类别树的展示
	 * @return
	 * @throws ServiceException
	 */
	public List getKnowledgeTreeExec() throws ServiceException{
		List<KnowledgeType> findTypeByParentId = knowledgeService.getAllTypes(null);
		return findTypeByParentId;
	}
	/*
	 * 取得页面传过来的列表参数
	 */
	public Map getPagerMap(){
		if(page==null) page ="1";
		if(sidx==null) sidx ="id";
		if(rows==null) rows ="10";
		if(sord==null) sord ="asc";
		Map filterMap = new HashMap();//存储参数的map
		filterMap.put("page", Integer.parseInt(page));
		filterMap.put("rows", Integer.parseInt(rows));
		filterMap.put("sidx", sidx);
		filterMap.put("sord", sord);
		return filterMap;
	}
	/**
	 * 得到往页面传的josn对象
	 * String
	 * 2011-4-26下午09:06:10
	 * author：李数
	 */
	public String getColumnJson(PageResult result,String[] viewArray){
		int rowsInt=Integer.parseInt(rows);
		int totalRows = result.getRowCount(); //所有的行数
		int totalPages = (totalRows + rowsInt - 1) / rowsInt; //总页数
		List rowAll = result.getData();   //查询出的所有记录
		JSONObject obj = new JSONObject();
		obj.put("page", ""+page);
		obj.put("total",totalPages);
		obj.put("records",""+totalRows);
		JSONArray lineitemArray = new JSONArray(); 
		Iterator it = rowAll.iterator();
		while (it.hasNext()) { 
			Object[] objlist = (Object[]) it.next();
			lineitemArray.add(getColumnValue(objlist,viewArray));
		} 
		obj.put("rows", lineitemArray);
		return obj.toString();
	}
	public JSONObject getColumnValue(Object[] objlist,String[] viewArray) {
		JSONObject objlineitem = new JSONObject();	
        for (int i=0;i<viewArray.length;i++)
        { 
        	if(!StringUtil.isEmpty(viewArray[i])){
        		if(objlist[i] != null ){
        			if(viewArray[i].indexOf(":")>0){
            			String realfieldName = viewArray[i].substring(0,viewArray[i].indexOf(":"));
            			String jsonStr = viewArray[i].substring(viewArray[i].indexOf(":")+1);
    	        		JSONArray json = JSONArray.fromObject(jsonStr);
    	        		JSONObject job = json.getJSONObject(0);
    	        		if(job.containsKey(objlist[i].toString())){
    	        			 String realvalue = job.getString(objlist[i].toString());
    		        		 if(realvalue!=null){
    		        			 objlineitem.put(realfieldName, realvalue.toString());
    		        		 }
    	        		}else{
    	        			 objlineitem.put(realfieldName, objlist[i].toString());
    	        		}
            		}else{
            			 if(objlist[i]!=null){
            				 objlineitem.put(viewArray[i], objlist[i].toString());
            			 } else {
            				 objlineitem.put(viewArray[i], "");
            				}
            		}
	        	}else {
					 objlineitem.put(viewArray[i], "");
				}
        	}
        }
		return objlineitem;
	}
	/**
	 * 功能描述：文件类别树型结构添加类别
	 * @return
	 * @throws IOException
	 * @throws ServiceException String
	 * @author 张海娜
	 * @date 2012-10-15 下午02:01:56
	 */
	public String saveKnowledgeExec() throws IOException, ServiceException{
		setContentType();
		String typeName = this.getRequest().getParameter("typeName");
		String typeDesc = this.getRequest().getParameter("typeDesc");
		Long parentId = Long.parseLong(this.getRequest().getParameter("parentId"));
		
		KnowledgeType parentknowledgeType = (KnowledgeType)epService.getDataById(KnowledgeType.class, parentId);
		Long levelindex = parentknowledgeType.getLevelindex()+1;
		//判断文件类别名称是否重复
		KnowledgeType knowledgeType = new KnowledgeType();
		knowledgeType.setParentId(parentId);
		knowledgeType.setTypeDesc(typeDesc);
		knowledgeType.setTypeName(typeName);
		knowledgeType.setLevelindex(levelindex);
		
		PrintWriter out = getResponse().getWriter();
		JSONObject jsonObject = new JSONObject();
		knowledgeService.saveKnowledgeType(knowledgeType);
		jsonObject.put("success",true);
		out.write(jsonObject.toString());
		
		return null;
	}
	/**
	 * 功能描述：文件类别树型结构修改类别功能
	 * @return
	 * @throws IOException
	 * @throws ServiceException
	 */
	public String updateKnowledgeExec() throws IOException, ServiceException{
		setContentType();
		Long mid = Long.parseLong(this.getRequest().getParameter("mid"));
		String typeName = this.getRequest().getParameter("typeName");
		String typeDesc = this.getRequest().getParameter("typeDesc");
		Long parentId = Long.parseLong(this.getRequest().getParameter("parentId"));
		KnowledgeType parentknowledgeType = (KnowledgeType)epService.getDataById(KnowledgeType.class, parentId);
		Long levelindex = parentknowledgeType.getLevelindex()+1;
		//判断试验型号名称是否重复
		KnowledgeType knowledgeType = new KnowledgeType();
		knowledgeType.setKnowledgeTypeId(mid);
		knowledgeType.setParentId(parentId);
		knowledgeType.setTypeDesc(typeDesc);
		knowledgeType.setTypeName(typeName);
		knowledgeType.setLevelindex(levelindex);
		PrintWriter out = getResponse().getWriter();
		JSONObject jsonObject = new JSONObject();
		knowledgeService.updateKnowledgeType(knowledgeType);
		jsonObject.put("success",true);
		out.write(jsonObject.toString());
		return null;
	}
	/**
	 * 功能描述：文件类别树型结构删除功能
	 * @return
	 * @throws IOException
	 * @throws ServiceException
	 */
	public String delExpTypeExec() throws IOException, ServiceException{
		setContentType();
		Long id = Long.parseLong(this.getRequest().getParameter("id"));
		PrintWriter out = getResponse().getWriter();
		JSONObject jsonObject = new JSONObject();
		knowledgeService.delKnowledgeType(id);
		jsonObject.put("success",true);
		logService.saveLogInfo(this.getLogBase(), "知识中心", "知识中心管理", "知识中心管理树形菜单删除成功");
		out.write(jsonObject.toString());
		return null;
	}
	/**
	 * 功能描述：文件类别树型结构添加类别
	 * @return
	 * @throws IOException
	 * @throws ServiceException
	 */
	public String saveKnowledgeTypeExec() throws IOException, ServiceException{
		setContentType();
		String typeName = this.getRequest().getParameter("typeName");
		String typeDesc = this.getRequest().getParameter("typeDesc");
		Long parentId = Long.parseLong(this.getRequest().getParameter("parentId"));
		
		//判断分类名称是否重复
		KnowledgeType knowledgeType = new KnowledgeType();
		knowledgeType.setParentId(parentId);
		knowledgeType.setTypeDesc(typeDesc);
		knowledgeType.setTypeName(typeName);
		
		PrintWriter out = getResponse().getWriter();
		JSONObject jsonObject = new JSONObject();
		if(knowledgeService.findByProperty(knowledgeType.getTypeName()).size()>0){
			getRequest().setAttribute("message", "名称已存在！");
			System.out.println("名称已存在");
			jsonObject.put("success",false);
			jsonObject.put("result", getText("名称已存在"));
			
//			logService.saveLogInfo(this.getLogBase(), "文件类别树添加类别失败", "知识中心", "");
		}else{
			knowledgeService.saveKnowledgeType(knowledgeType);
			jsonObject.put("success",true);
//			logService.saveLogInfo(this.getLogBase(), "文件类别树添加类别成功", "知识中心", "");
			logService.saveLogInfo(this.getLogBase(), "知识中心", "知识中心管理", "知识中心管理树形菜单添加成功");
		}
		out.write(jsonObject.toString());
		return null;
	}
	/**
	 * 功能描述：文件类别树型结构修改类别功能
	 * @return
	 * @throws IOException
	 * @throws ServiceException
	 */
	public String updateKnowledgeTypeExec() throws IOException, ServiceException{
		setContentType();
		Long id = Long.parseLong(this.getRequest().getParameter("id"));
		String typeName = this.getRequest().getParameter("typeName");
		String typeDesc = this.getRequest().getParameter("typeDesc");
		Long parentId = Long.parseLong(this.getRequest().getParameter("parentId"));
		
		//判断分类名称是否重复
		KnowledgeType knowledgeType = new KnowledgeType();
		knowledgeType.setKnowledgeTypeId(id);
		knowledgeType.setParentId(parentId);
		knowledgeType.setTypeDesc(typeDesc);
		knowledgeType.setTypeName(typeName);
		
		PrintWriter out = getResponse().getWriter();
		JSONObject jsonObject = new JSONObject();
		if(knowledgeService.findByProperty(knowledgeType.getTypeName()).size()>0){
			jsonObject.put("success",false);
			jsonObject.put("result", getText("名称已存在"));
//			logService.saveLogInfo(this.getLogBase(), "文件类别树修改类别失败", "知识中心", "");
		}else{
			knowledgeService.updateKnowledgeType(knowledgeType);
			jsonObject.put("success",true);
//			logService.saveLogInfo(this.getLogBase(), "文件类别树修改类别成功", "知识中心", "");
			logService.saveLogInfo(this.getLogBase(), "知识中心", "知识中心管理", "知识中心管理树形菜单修改成功");
		}
		out.write(jsonObject.toString());
		return null;
	}
	/**
	 * 功能描述：文件类别树型结构删除功能
	 * @return
	 * @throws IOException
	 * @throws ServiceException
	 */
	public String delKnowledgeTypeExec() throws IOException, ServiceException{
		PrintWriter out = getResponse().getWriter();
		try {
			setContentType();
			Long id = Long.parseLong(this.getRequest().getParameter("id"));
			Map<String, String> filterMap = getPagerMap();
			PageResult count=knowledgeService.getCountKnowledgeTypeByFileType(id, filterMap);
			if(count.getRowCount() == 0){
				knowledgeService.delKnowledgeType(id);
				out.print("{\"ajaxResult\":\"success\"}");
			}else{
				out.print("{\"ajaxResult\":\"have\"}");
			}
			//jsonObject.put("success",true);
//			logService.saveLogInfo(this.getLogBase(), "文件类别树删除类别成功", "知识中心", "");
			logService.saveLogInfo(this.getLogBase(), "知识中心", "知识中心管理", "知识中心管理树形菜单删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			out.print("{\"ajaxResult\":\"error\"}");
		}
		return null;
	}
	/**
	 * 功能描述：添加标签功能，保存数据库
	 * @return
	 * @throws IOException
	 * @throws ServiceException
	 */
	public String addTypeExec() throws IOException, ServiceException{
		setContentType();
		String biaoqian = this.getRequest().getParameter("biaoqian");
		
		//判断分类名称是否重复
		KnowFileType knowFileType = new KnowFileType();
		knowFileType.setTypeDesc(biaoqian);
		knowFileType.setTypeName(biaoqian);
		
		PrintWriter out = getResponse().getWriter();
		JSONObject jsonObject = new JSONObject();
		if(knowledgeService.findByFileName(knowFileType.getTypeName()).size()>0){
			out.print("{\"ajaxResult\":\"have\"}");
			//getRequest().setAttribute("message", "标签已存在！");
			//jsonObject.put("success",false);
			//jsonObject.put("result", getText("标签已存在！"));
//			logService.saveLogInfo(this.getLogBase(), "添加标签功能失败", "知识中心", "");
		}else{
			Long biaoqianid = knowledgeService.saveKnowFileType(knowFileType);
			knowFileType = knowledgeService.findknowFileType(biaoqianid);
			jsonObject.put("success",true);
			jsonObject.put("id",biaoqianid);
			jsonObject.put("name",knowFileType.getTypeName());
			out.print(jsonObject.toString());
//			logService.saveLogInfo(this.getLogBase(), "添加标签功能成功", "知识中心", "");
			logService.saveLogInfo(this.getLogBase(), "知识中心", "知识中心管理", "知识中心管理标签添加成功");
		}
		return null;
	}
	/**
	 * 功能描述：根据标签名称查找该标签
	 * @return
	 * @throws IOException
	 * @throws ServiceException
	 */
	public String searchTypeExec() throws IOException, ServiceException{
		setContentType();
		String biaoqian = this.getRequest().getParameter("biaoqian");
		PrintWriter out = getResponse().getWriter();
		JSONObject jsonObject = new JSONObject();
		//分页查找数据库中所有的标签
		Long start = null;
		if(this.getRequest().getParameter("start")!=null)
			start = Long.parseLong(this.getRequest().getParameter("start"));
		Long limit = null;
		if(this.getRequest().getParameter("limit")!=null)
			limit = Long.parseLong(this.getRequest().getParameter("limit"));
		String fileName = null;
		if(!StringUtil.isEmpty(biaoqian))
			fileName = biaoqian;
		
		List<KnowFileType> knowFileType = knowledgeService.getKnowFileType(fileName,start,limit);
		jsonObject.put("success",true);
		jsonObject.put("fileDocumentType",JSONArray.fromObject(knowFileType));
//		logService.saveLogInfo(this.getLogBase(), "标签查询功能成功", "知识中心", "");
		out.write(jsonObject.toString());
		return null;
	}
	
	/**
	 * 添加文件页面
	 * @return
	 * @throws IOException
	 * @throws ServiceException
	 */
	public String addFileManageExec() throws IOException, ServiceException{
		setContentType();
		//判断文件id是否为空，如果为空，表示增加操作，否则为修改，删除，或者查看操作
		if(knowledgeManage!=null&&!StringUtil.isEmpty(knowledgeManage.getId().toString()))   //id不为空，修改操作
		{
			Map<String, String> filterMap = getPagerMap();
			filterMap.put("id", knowledgeManage.getId().toString());
			List<KnowledgeManage> kmList = knowledgeService.findByProperty(filterMap);
			for (Iterator it = kmList.iterator(); it.hasNext();) {
				knowledgeManage = (KnowledgeManage) it.next();
			}
			this.getRequest().setAttribute("knowledgeManage", knowledgeManage);
			this.getRequest().setAttribute("id", knowledgeManage.getId());
		}
		//根据选中文件分类的id查询相关的名称信息
		if(knowledgeManage==null&&!StringUtil.isEmpty(treeId))
		{
			Map<String, String> filterMap = getPagerMap();
			filterMap.put("knowledgeTypeId", treeId);
			List<KnowledgeType> allTypes = knowledgeService.getAllTypes(filterMap);
			if(allTypes.size()>0)
			{
				KnowledgeType knowledgeType = allTypes.get(0);
				this.getRequest().setAttribute("efileDocumenttypeId", treeId);
				this.getRequest().setAttribute("efileDocumenttypeName", knowledgeType.getTypeName());
			}
		}else if(knowledgeManage!=null){
			Map<String, String> filterMap = getPagerMap();
			filterMap.put("knowledgeTypeId", knowledgeManage.getFileType());
			List<KnowledgeType> allTypes = knowledgeService.getAllTypes(filterMap);
			if(allTypes.size()>0)
			{
				KnowledgeType knowledgeType = allTypes.get(0);
				this.getRequest().setAttribute("efileDocumenttypeId", knowledgeType.getKnowledgeTypeId());
				this.getRequest().setAttribute("efileDocumenttypeName", knowledgeType.getTypeName());
			}
			
		}
		Map<String, String> filterMap = getPagerMap();
		PageResult count=knowledgeService.getAllKnowFileType(filterMap);
		int totalPageNum = ((count.getRowCount()  +  5  - 1) / 5); 
		this.getRequest().setAttribute("totalPageNum", String.valueOf(totalPageNum));
		//查找数据库中所有的标签
		Long start = 0l;
		Long limit = 5l;
		List<KnowFileType> knowFileType = knowledgeService.getKnowFileType(null,start,limit);
		this.getRequest().setAttribute("vfileDocumenttype", JSONArray.fromObject(knowFileType));
		this.getRequest().setAttribute("operator",operator);
		if("view".equals(operator))
		{
//			logService.saveLogInfo(this.getLogBase(), "查看文件功能成功", "知识中心", "");
			return "VIEW";
		}else if("update".equals(operator)){
//			logService.saveLogInfo(this.getLogBase(), "修改文件功能成功", "知识中心", "");
			return "SUCCESS";
		}else{
//			logService.saveLogInfo(this.getLogBase(), "添加文件功能成功", "知识中心", "");
			return "SUCCESS";
		}
	}
	/**
	 * 修改文件页面
	 * @return
	 * @throws IOException
	 * @throws ServiceException
	 */
	public String updateFileManageExec() throws IOException, ServiceException{
		setContentType();
		//判断文件id是否为空，如果为空，表示增加操作，否则为修改，删除，或者查看操作
		if(knowledgeManage!=null&&!StringUtil.isEmpty(knowledgeManage.getId().toString()))   //id不为空，修改操作
		{
			Map<String, String> filterMap = getPagerMap();
			filterMap.put("id", knowledgeManage.getId().toString());
			List<KnowledgeManage> kmList = knowledgeService.findByProperty(filterMap);
			for (Iterator it = kmList.iterator(); it.hasNext();) {
				knowledgeManage = (KnowledgeManage) it.next();
			}
			this.getRequest().setAttribute("knowledgeManage", knowledgeManage);
			this.getRequest().setAttribute("id", knowledgeManage.getId());
			
		}
		//根据选中文件分类的id查询相关的名称信息
		if(knowledgeManage==null&&!StringUtil.isEmpty(treeId))
		{
			Map<String, String> filterMap = getPagerMap();
			filterMap.put("knowledgeTypeId", treeId);
			List<KnowledgeType> allTypes = knowledgeService.getAllTypes(filterMap);
			if(allTypes.size()>0)
			{
				KnowledgeType knowledgeType = allTypes.get(0);
				this.getRequest().setAttribute("efileDocumenttypeId", treeId);
				this.getRequest().setAttribute("efileDocumenttypeName", knowledgeType.getTypeName());
			}
		}else if(knowledgeManage!=null){
			Map<String, String> filterMap = getPagerMap();
			filterMap.put("knowledgeTypeId", knowledgeManage.getFileType());
			List<KnowledgeType> allTypes = knowledgeService.getAllTypes(filterMap);
			if(allTypes.size()>0)
			{
				KnowledgeType knowledgeType = allTypes.get(0);
				this.getRequest().setAttribute("efileDocumenttypeId", knowledgeType.getKnowledgeTypeId());
				this.getRequest().setAttribute("efileDocumenttypeName", knowledgeType.getTypeName());
			}
			
		}
		Map<String, String> filterMap = getPagerMap();
		PageResult count=knowledgeService.getAllKnowFileType(filterMap);
		int totalPageNum = ((count.getRowCount()  +  5  - 1) / 5); 
		this.getRequest().setAttribute("totalPageNum", String.valueOf(totalPageNum));
		//查找数据库中所有的标签
		Long start = 0l;
		Long limit = 5l;
		
		String biaoqian = this.getRequest().getParameter("biaoqian");
		String fileName = null;
		if(!StringUtil.isEmpty(biaoqian))
			fileName = biaoqian;
		Long id = knowledgeManage.getId();
		List<KnowFileType> knowFileTypeName = knowledgeService.getFileTypeName(fileName,id);
		this.getRequest().setAttribute("haveEnd", JSONArray.fromObject(knowFileTypeName));
		
		List<KnowFileType> knowFileType = knowledgeService.getKnowFileType(null,start,limit);
		this.getRequest().setAttribute("vfileDocumenttype", JSONArray.fromObject(knowFileType));
		this.getRequest().setAttribute("operator",operator);
		

		if("view".equals(operator))
		{
//			logService.saveLogInfo(this.getLogBase(), "查看文件功能成功", "知识中心", "");
			return "VIEW";
		}else if("update".equals(operator)){
			this.getRequest().setAttribute("editid", knowledgeManage.getId());
//			logService.saveLogInfo(this.getLogBase(), "修改文件功能成功", "知识中心", "");
			return "SUCCESS";
		}else{
//			logService.saveLogInfo(this.getLogBase(), "添加文件功能成功", "知识中心", "");
			return "SUCCESS";
		}
	}
	/**
	 * 查看文件页面
	 * @return
	 * @throws IOException
	 * @throws ServiceException
	 */
	public String viewFileManageExec() throws IOException, ServiceException{
		setContentType();
		//判断文件id是否为空，如果为空，表示增加操作，否则为修改，删除，或者查看操作
		if(knowledgeManage!=null&&!StringUtil.isEmpty(knowledgeManage.getId().toString()))   //id不为空，修改操作
		{
			Map<String, String> filterMap = getPagerMap();
			filterMap.put("id", knowledgeManage.getId().toString());
			List<KnowledgeManage> kmList = knowledgeService.findByProperty(filterMap);
			for (Iterator it = kmList.iterator(); it.hasNext();) {
				knowledgeManage = (KnowledgeManage) it.next();
			}
			this.getRequest().setAttribute("knowledgeManage", knowledgeManage);
			this.getRequest().setAttribute("id", knowledgeManage.getId());
		}
		//根据选中文件分类的id查询相关的名称信息
		if(knowledgeManage==null&&!StringUtil.isEmpty(treeId))
		{
			Map<String, String> filterMap = getPagerMap();
			filterMap.put("knowledgeTypeId", treeId);
			List<KnowledgeType> allTypes = knowledgeService.getAllTypes(filterMap);
			if(allTypes.size()>0)
			{
				KnowledgeType knowledgeType = allTypes.get(0);
				this.getRequest().setAttribute("efileDocumenttypeId", treeId);
				this.getRequest().setAttribute("efileDocumenttypeName", knowledgeType.getTypeName());
			}
		}else if(knowledgeManage!=null){
			Map<String, String> filterMap = getPagerMap();
			filterMap.put("knowledgeTypeId", knowledgeManage.getFileType());
			List<KnowledgeType> allTypes = knowledgeService.getAllTypes(filterMap);
			if(allTypes.size()>0)
			{
				KnowledgeType knowledgeType = allTypes.get(0);
				this.getRequest().setAttribute("efileDocumenttypeId", knowledgeType.getKnowledgeTypeId());
				this.getRequest().setAttribute("efileDocumenttypeName", knowledgeType.getTypeName());
			}
			
		}
		
		//查找数据库中所有的标签
		Long start = 0l;
		Long limit = 5l;
		
		String biaoqian = this.getRequest().getParameter("biaoqian");
		String fileName = null;
		if(!StringUtil.isEmpty(biaoqian))
			fileName = biaoqian;
		Long id = knowledgeManage.getId();
		List<KnowFileType> knowFileTypeName = knowledgeService.getFileTypeName(fileName,id);
		this.getRequest().setAttribute("haveEnd", JSONArray.fromObject(knowFileTypeName));
		
		List<KnowFileType> knowFileType = knowledgeService.getKnowFileType(null,start,limit);
		this.getRequest().setAttribute("vfileDocumenttype", JSONArray.fromObject(knowFileType));
		this.getRequest().setAttribute("operator",operator);
		if("view".equals(operator))
		{
//			logService.saveLogInfo(this.getLogBase(), "查看文件功能成功", "知识中心", "");
			this.getRequest().setAttribute("flag",this.getRequest().getParameter("flag"));
			return "VIEW";
		}else if("update".equals(operator)){
//			logService.saveLogInfo(this.getLogBase(), "修改文件功能成功", "知识中心", "");
			return "SUCCESS";
		}else{
//			logService.saveLogInfo(this.getLogBase(), "添加文件功能成功", "知识中心", "");
			return "SUCCESS";
		}
	}
	public String delFileManageExec() throws IOException, ServiceException{
		setContentType();
		String ids = this.getRequest().getParameter("id");
		
		PrintWriter out = getResponse().getWriter();
		JSONObject jsonObject = new JSONObject();
		if(ids.indexOf(",")==-1){
			knowledgeService.delKnowledgeManage(Long.parseLong(ids));
			List<KnowledgeHandOut>  knowledgeHandOutlist = knowledgeService.findKnowledgeHanderOut(Long.parseLong(ids));
			String str = "";
			for (int j = 0; j < knowledgeHandOutlist.size(); j++) {
				str += knowledgeHandOutlist.get(j).getId()+",";
			}
			String knowledgeHandOutids = str.substring(0,str.length()-1);
			if(knowledgeHandOutids.indexOf(",")==-1){
				knowledgeService.delknowledgeHandOut(Long.parseLong(knowledgeHandOutids));
			}else{
				String[] knowledgeHandOut = knowledgeHandOutids.split(",");
				for (int h = 0; h < knowledgeHandOut.length; h++) {
					
					knowledgeService.delknowledgeHandOut(Long.parseLong(knowledgeHandOut[h]));
				}
			}
		}else{
			String[] split = ids.split(",");
			for (int i = 0; i < split.length; i++) {
				
				knowledgeService.delKnowledgeManage(Long.parseLong(split[i]));
				List<KnowledgeHandOut>  knowledgeHandOutlist = knowledgeService.findKnowledgeHanderOut(Long.parseLong(split[i]));
				String str = "";
				for (int j = 0; j < knowledgeHandOutlist.size(); j++) {
					str += knowledgeHandOutlist.get(j).getId()+",";
				}
				String knowledgeHandOutids = str.substring(0,str.length()-1);
				if(knowledgeHandOutids.indexOf(",")==-1){
					knowledgeService.delknowledgeHandOut(Long.parseLong(knowledgeHandOutids));
				}else{
					String[] knowledgeHandOut = knowledgeHandOutids.split(",");
					for (int h = 0; h < knowledgeHandOut.length; h++) {
						
						knowledgeService.delknowledgeHandOut(Long.parseLong(knowledgeHandOut[h]));
					}
				}
				
			}
		}
		
		jsonObject.put("success",true);
//		logService.saveLogInfo(this.getLogBase(), "文件类别树删除类别成功", "知识中心", "");
		out.write(jsonObject.toString());
		return null;
	}
	/**
	 *  功能描述：添加修改后查询文件列表，(userid过滤)
	 * @author zhn
	 * @date 2013-11-8 下午04:22:40
	 * @throws IOException
	 * @throws ServiceException
	 */
	public void getAllKnowledgeManageAfterByUserIdExec() throws IOException, ServiceException{
		setContentType();
		try {
			Map<String, String> filterMap = getPagerMap();
			String userid = ServletActionContext.getRequest().getSession().getAttribute("TDM_USER_ID").toString();
			
			//取出页面中的查询条件
			String fileType = getRequest().getParameter("fileType");
			String fileName =StringUtil.encodingUrl(getRequest().getParameter("fileName"));
			String fileSampleId =StringUtil.encodingUrl(getRequest().getParameter("fileSampleId"));
			String biaoqian =StringUtil.encodingUrl(getRequest().getParameter("biaoqian"));
			
			//将查询条件保存到Map里
			if(!StringUtil.isEmpty(fileType)  && !fileType.equals("1"))
				filterMap.put("fileType", fileType);
			if(!StringUtil.isEmpty(fileName))
				filterMap.put("fileName", fileName);
			if(!StringUtil.isEmpty(fileSampleId))
				filterMap.put("fileSampleId", fileSampleId);
			if(!StringUtil.isEmpty(biaoqian))
				filterMap.put("fileDocumentType", biaoqian);
			if(!StringUtil.isEmpty(userid))
				filterMap.put("userid", userid);
			String[] viewArray = { "id", "FILE_NAME","FILE_SAMPLEID","fileDocumentType","file_documenttype_name", "fileEntryAddress", "fileAuthor", "fileVersion",
					"filePage","fileVolumenumber","fileCustos","fileClassificationType","filePrice","filePublicationDate","fileWriteDate",
					"fileIncorporateDate","filePublishingCompany","fileSubject","fileSummary","fileRemarks","fileAuditor","fileCheckDate",
					"fileLookNewDays","fileLookNewDate","fileCheckSuggestion","fileIsPublic","fileType","fileAdd","SETUP_USER_NAME","SETUP_TIME"};
			// 查询所有的数据
			PageResult result = knowledgeService.getAllKnowledgeManageAfterByUserIdExec(filterMap);
			String returnStr = getColumnJson(result, viewArray);

			PrintWriter out = getResponse().getWriter();
			out.print(returnStr);
//			logService.saveLogInfo(this.getLogBase(), "文件列表查询成功", "知识中心", "");
		} catch (Exception e) {
			e.printStackTrace();
//			logService.saveLogInfo(this.getLogBase(), "文件列表查询失败", "知识中心", "");
		}
		
		
	}
	/**
	 * 功能描述：添加修改后查询文件列表，
	 * @throws IOException
	 * @throws ServiceException
	 */
	public void getAllKnowledgeManageAfterExec() throws IOException, ServiceException{
		setContentType();
		try {
			Map<String, String> filterMap = getPagerMap();
			//取出页面中的查询条件
			String fileType = getRequest().getParameter("fileType");
			String fileName =StringUtil.encodingUrl(getRequest().getParameter("fileName"));
			String fileSampleId =StringUtil.encodingUrl(getRequest().getParameter("fileSampleId"));
			String biaoqian =StringUtil.encodingUrl(getRequest().getParameter("biaoqian"));
			
			//将查询条件保存到Map里
			if(!StringUtil.isEmpty(fileType))
				filterMap.put("fileType", fileType);
			if(!StringUtil.isEmpty(fileName))
				filterMap.put("fileName", fileName);
			if(!StringUtil.isEmpty(fileSampleId))
				filterMap.put("fileSampleId", fileSampleId);
			if(!StringUtil.isEmpty(biaoqian))
				filterMap.put("fileDocumentType", biaoqian);
			
			String[] viewArray = { "id", "fileName","fileSampleId","fileDocumentType","fileDocumentTypeName", "fileEntryAddress", "fileAuthor", "fileVersion",
					"filePage","fileVolumenumber","fileCustos","fileClassificationType","filePrice","filePublicationDate","fileWriteDate",
					"fileIncorporateDate","filePublishingCompany","fileSubject","fileSummary","fileRemarks","fileAuditor","fileCheckDate",
					"fileLookNewDays","fileLookNewDate","fileCheckSuggestion","fileIsPublic","fileType","fileAdd","setupUserName","setupTime"};
			// 查询所有的数据
			PageResult result = knowledgeService.queryKnowledgeManageAfterList(fileType,filterMap);
			String returnStr = getColumnJson(result, viewArray);

			PrintWriter out = getResponse().getWriter();
			out.print(returnStr);
//			logService.saveLogInfo(this.getLogBase(), "文件列表查询成功", "知识中心", "");
		} catch (Exception e) {
			e.printStackTrace();
//			logService.saveLogInfo(this.getLogBase(), "文件列表查询失败", "知识中心", "");
		}
		
		
	}
	/**
	 * 功能描述：查询文件列表，
	 * @throws IOException
	 * @throws ServiceException
	 */
	public void getAllKnowledgeManageExec() throws IOException, ServiceException{
		setContentType();
		try {
			Map<String, String> filterMap = getPagerMap();
			//取出页面中的查询条件
			String fileType = getRequest().getParameter("fileType");
			String fileName =StringUtil.encodingUrl(getRequest().getParameter("fileName"));
			String fileSampleId =StringUtil.encodingUrl(getRequest().getParameter("fileSampleId"));
			String biaoqian =StringUtil.encodingUrl(getRequest().getParameter("biaoqian"));
			
			//将查询条件保存到Map里
			if(!StringUtil.isEmpty(fileType))
				filterMap.put("fileType", fileType);
			if(!StringUtil.isEmpty(fileName))
				filterMap.put("fileName", fileName);
			if(!StringUtil.isEmpty(fileSampleId))
				filterMap.put("fileSampleId", fileSampleId);
			if(!StringUtil.isEmpty(biaoqian))
				filterMap.put("fileDocumentTypeName", biaoqian);
			
			String[] viewArray = { "id", "FILE_NAME","FILE_SAMPLEID","fileDocumentType","file_documenttype_name", "fileEntryAddress", "fileAuthor", "fileVersion",
					"filePage","fileVolumenumber","fileCustos","fileClassificationType","filePrice","filePublicationDate","fileWriteDate",
					"fileIncorporateDate","filePublishingCompany","fileSubject","fileSummary","fileRemarks","fileAuditor","fileCheckDate",
					"fileLookNewDays","fileLookNewDate","fileCheckSuggestion","fileIsPublic","fileType","fileAdd","SETUP_USER_NAME","SETUP_TIME"};
			// 查询所有的数据
			PageResult result = knowledgeService.queryKnowledgeManageList(filterMap);
			String returnStr = getColumnJson(result, viewArray);
			
			PrintWriter out = getResponse().getWriter();
			out.print(returnStr);
//			logService.saveLogInfo(this.getLogBase(), "文件列表查询成功", "知识中心", "");
		} catch (Exception e) {
			e.printStackTrace();
//			logService.saveLogInfo(this.getLogBase(), "文件列表查询失败", "知识中心", "");
		}
		
		
	}
	/**
	 * 我的收藏 查询方法
	 * @author zhn
	 * @date 2013-11-8 上午10:29:27
	 * @throws IOException
	 * @throws ServiceException
	 */
	public void getAllKnowledgeManageByUserIdExec() throws IOException, ServiceException{
		setContentType();
		try {
			Map<String, String> filterMap = getPagerMap();
			String userid = ServletActionContext.getRequest().getSession().getAttribute("TDM_USER_ID").toString();
			//取出页面中的查询条件
			String fileType = getRequest().getParameter("fileType");
			String fileName =StringUtil.encodingUrl(getRequest().getParameter("fileName"));
			String fileSampleId =StringUtil.encodingUrl(getRequest().getParameter("fileSampleId"));
			String biaoqian =StringUtil.encodingUrl(getRequest().getParameter("biaoqian"));
			
			//将查询条件保存到Map里
			if(!StringUtil.isEmpty(fileType))
				filterMap.put("fileType", fileType);
			if(!StringUtil.isEmpty(fileName))
				filterMap.put("fileName", fileName);
			if(!StringUtil.isEmpty(fileSampleId))
				filterMap.put("fileSampleId", fileSampleId);
			if(!StringUtil.isEmpty(biaoqian))
				filterMap.put("fileDocumentTypeName", biaoqian);
			if(!StringUtil.isEmpty(userid))
				filterMap.put("userid", userid);
			String[] viewArray = { "id", "FILE_NAME","FILE_SAMPLEID","fileDocumentType","file_documenttype_name", "fileEntryAddress", "fileAuthor", "fileVersion",
					"filePage","fileVolumenumber","fileCustos","fileClassificationType","filePrice","filePublicationDate","fileWriteDate",
					"fileIncorporateDate","filePublishingCompany","fileSubject","fileSummary","fileRemarks","fileAuditor","fileCheckDate",
					"fileLookNewDays","fileLookNewDate","fileCheckSuggestion","fileIsPublic","fileType","fileAdd","SETUP_USER_NAME","SETUP_TIME"};
			// 查询所有的数据
			PageResult result = knowledgeService.getAllKnowledgeManageShouCangExec(filterMap);
			String returnStr = getColumnJson(result, viewArray);
			
			PrintWriter out = getResponse().getWriter();
			out.print(returnStr);
//			logService.saveLogInfo(this.getLogBase(), "文件列表查询成功", "知识中心", "");
		} catch (Exception e) {
			e.printStackTrace();
//			logService.saveLogInfo(this.getLogBase(), "文件列表查询失败", "知识中心", "");
		}
		
		
	}
	/**
	 * 初始化查看文档
	 * @author zhn
	 * @date 2013-11-8 下午02:00:15
	 * @return
	 * @throws Exception
	 */
	public String chaKanWenDangExec() throws Exception{
		try{
			String labels = ",";
			String id = this.getRequest().getParameter("id");
			knowledgeManage = knowledgeService.findFileAddressById(Long.parseLong(id));
			String fileAdd = knowledgeManage.getFileAdd();
			String address = knowledgeManage.getFileEntryAddress();
			String fileaddress ="";
			this.getRequest().setAttribute("id", id);
			//this.getRequest().setAttribute("flowname", flowtype.getFlowname());
			if(address.indexOf("/")==-1){
				fileaddress = fileAdd +"/"+knowledgeManage.getFileEntryAddress();
				
			}else{
				fileaddress = knowledgeManage.getFileEntryAddress();
			}
			this.getRequest().setAttribute("fileaddress", fileaddress);
			//this.getRequest().setAttribute("labels", labelsjson.toString());
			String name = "";
			String extention = "";
			if(fileaddress.length()>0 && fileaddress!=null){  //--截取文件名
				int i = fileaddress.lastIndexOf(".");
				if(i>-1 && i<fileaddress.length()){
					name = fileaddress.substring(0, i); //--文件名
					extention = fileaddress.substring(i+1); //--扩展名
				}
			} 
			if("docx".equals(extention) || "doc".equals(extention)){
				return "chaKanWenDangWord";
			}else if("xlsx".equals(extention) || "xls".equals(extention)){
				System.out.println("xls");
				return "chaKanWenDangElsx";
			}else{
				return "viewOtherFile";
			}
			
		}catch(Exception exp){
			exp.printStackTrace();
			return "chaKanWenDang";
		}
	}
	
	/**
	 * 通过父节点id查询文件列表，(userid过滤)
	 * @author zhn
	 * @date 2013-11-8 下午04:47:44
	 * @throws IOException
	 * @throws ServiceException
	 */
	public void getAllKnowledgeManageByPatentIdByUserIdExec() throws IOException, ServiceException{
		setContentType();
		try {
			Map<String, String> filterMap = getPagerMap();
			String userid = ServletActionContext.getRequest().getSession().getAttribute("TDM_USER_ID").toString();
			//取出页面中的查询条件
			String fileType = getRequest().getParameter("fileType");
			String fileName =getRequest().getParameter("fileName");
			String fileSampleId =getRequest().getParameter("fileSampleId");
			String biaoqian =getRequest().getParameter("biaoqian");
			
			//将查询条件保存到Map里
			if(!StringUtil.isEmpty(fileType))
				filterMap.put("fileType", fileType);
			if(!StringUtil.isEmpty(fileName))
				filterMap.put("fileName", fileName);
			if(!StringUtil.isEmpty(fileSampleId))
				filterMap.put("fileSampleId", fileSampleId);
			if(!StringUtil.isEmpty(biaoqian))
				filterMap.put("fileDocumentType", biaoqian);
			if(!StringUtil.isEmpty(userid))
				filterMap.put("userid", userid);
			String[] viewArray = { "id", "FILE_NAME","FILE_SAMPLEID","fileDocumentType","file_documenttype_name", "fileEntryAddress", "fileAuthor", "fileVersion",
					"filePage","fileVolumenumber","fileCustos","fileClassificationType","filePrice","filePublicationDate","fileWriteDate",
					"fileIncorporateDate","filePublishingCompany","fileSubject","fileSummary","fileRemarks","fileAuditor","fileCheckDate",
					"fileLookNewDays","fileLookNewDate","fileCheckSuggestion","fileIsPublic","fileType","fileAdd","SETUP_USER_NAME","SETUP_TIME"};
			// 查询所有的数据
			PageResult result = knowledgeService.getAllKnowledgeManageByPatentIdByUserIdExec(fileType,filterMap);
			String returnStr = getColumnJson(result, viewArray);

			PrintWriter out = getResponse().getWriter();
			out.print(returnStr);
//			logService.saveLogInfo(this.getLogBase(), "文件列表查询成功", "知识中心", "");
		} catch (Exception e) {
			e.printStackTrace();
//			logService.saveLogInfo(this.getLogBase(), "文件列表查询失败", "知识中心", "");
		}
		
		
	}
	/**
	 * 功能描述：通过父节点id查询文件列表，
	 * @throws IOException
	 * @throws ServiceException
	 */
	public void getAllKnowledgeManageByPatentIdExec() throws IOException, ServiceException{
		setContentType();
		try {
			Map<String, String> filterMap = getPagerMap();
			//取出页面中的查询条件
			String fileType = getRequest().getParameter("fileType");
			String fileName =getRequest().getParameter("fileName");
			String fileSampleId =getRequest().getParameter("fileSampleId");
			String biaoqian =getRequest().getParameter("biaoqian");
			
			//将查询条件保存到Map里
			if(!StringUtil.isEmpty(fileType))
				filterMap.put("fileType", fileType);
			if(!StringUtil.isEmpty(fileName))
				filterMap.put("fileName", fileName);
			if(!StringUtil.isEmpty(fileSampleId))
				filterMap.put("fileSampleId", fileSampleId);
			if(!StringUtil.isEmpty(biaoqian))
				filterMap.put("fileDocumentType", biaoqian);
			
			String[] viewArray = { "id", "FILE_NAME","FILE_SAMPLEID","file_documenttype_name","fileDocumentTypeName", "fileEntryAddress", "fileAuthor", "fileVersion",
					"filePage","fileVolumenumber","fileCustos","fileClassificationType","filePrice","filePublicationDate","fileWriteDate",
					"fileIncorporateDate","filePublishingCompany","fileSubject","fileSummary","fileRemarks","fileAuditor","fileCheckDate",
					"fileLookNewDays","fileLookNewDate","fileCheckSuggestion","fileIsPublic","fileType","fileAdd","SETUP_USER_NAME","SETUP_TIME"};
			// 查询所有的数据
			PageResult result = knowledgeService.queryKnowledgeManageListByPatentId(fileType,filterMap);
			String returnStr = getColumnJson(result, viewArray);

			PrintWriter out = getResponse().getWriter();
			out.print(returnStr);
//			logService.saveLogInfo(this.getLogBase(), "文件列表查询成功", "知识中心", "");
		} catch (Exception e) {
			e.printStackTrace();
//			logService.saveLogInfo(this.getLogBase(), "文件列表查询失败", "知识中心", "");
		}
		
		
	}
	/**
	 * 判断文件类别下是否有文件
	 * @throws IOException
	 * @throws ServiceException void
	 * @author 张海娜
	 * @date 2012-10-19 下午06:30:38
	 */
	public void getCountKnowledgeTypeByFileType() throws IOException, ServiceException{
		setContentType();
		try {
			Map<String, String> filterMap = getPagerMap();
			PageResult result = knowledgeService.getCountKnowledgeTypeByFileType(id,filterMap);
			PrintWriter out = getResponse().getWriter();
			out.print(result);
//			logService.saveLogInfo(this.getLogBase(), "文件列表查询成功", "知识中心", "");
		} catch (Exception e) {
			e.printStackTrace();
//			logService.saveLogInfo(this.getLogBase(), "文件列表查询失败", "知识中心", "");
		}
		
		
	}
	/**
	 * 功能描述：涉及到文件上传（增加方法）
	 * @return
	 * @throws IOException
	 * @throws ServiceException 
	 */
	public String addFileExec() throws IOException, ServiceException{
		setContentType();
		
		String contentType = this.getRequest().getContentType();
		String treeName = getRequest().getParameter("treeName");
		
		//System.out.println(contentType); 
		if(knowledgeManage!=null)
		{
			if(contentType==null || !contentType.startsWith("multipart/form-data"))  //没有文件提交
			{
				//knowledgeService.saveKnowledgeManage(knowledgeManage);
			}else{
				List files = getUpload();
				StringBuilder sb = new StringBuilder();
				for (int i = 0 ; i < files.size() ; i++) 
				{ 
					Timestamp date = new Timestamp(System.currentTimeMillis());
					 String Date = new SimpleDateFormat("yyyyMMdd").format(date);
					 String Date1 =Date.substring(0, Date.length());
					String filePath =  (String) this.getRequest().getSession().getAttribute("SYS_FILEPATH");
					String str = filePath.replaceAll("\\\\", "/");   
					String url = str + "/taskfile/knowledgeCenter/"+treeName+"/"+Date1;
					knowledgeManage.setFileAdd(url);
					File file = new File(url);
					if ( ! (file.exists())  &&   ! (file.isDirectory()))  {
		                 boolean  creadok  =  file.mkdirs();
		                 if (creadok)  {
		                    System.out.println( " ok:创建文件夹成功！ " );
		                } else  {
		                    System.out.println( " err:创建文件夹失败！ " );                    
		                } 
		            } 
					String address = url + "/" + getUploadFileName().get(i);
					FileOutputStream fos = new FileOutputStream(address); 
					FileInputStream fis = new FileInputStream(files.get(i).toString()); 
					byte[] buffer = new byte[1024]; 
					int len = 0; 
					while ((len = fis.read(buffer)) > 0) 
					{ 
						fos.write(buffer, 0, len);
					} 
					sb.append(getUploadFileName().get(i));
					knowledgeManage.setFileEntryAddress(address);
				}
			}
			String userId = getRequest().getSession().getAttribute("TDM_USER_ID").toString();
			String userName = getRequest().getSession().getAttribute("username").toString();
			Timestamp date = new Timestamp(System.currentTimeMillis());
			 String Date = new SimpleDateFormat("yyyy-MM-dd").format(date);
			 String Date1 =Date.substring(0, Date.length());
			knowledgeManage.setSetupUserId(Long.parseLong(userId));
			knowledgeManage.setSetupUserName(userName);
			knowledgeManage.setSetupTime(date);
			
			String haveEnd = getRequest().getParameter("haveEnd");
			String haveEndName ="";
			if(!haveEnd.equals("")){
				
				String[] haveEndList = haveEnd.split(",");
				for (String haveEndId : haveEndList) {
					KnowFileType knowFileType  = knowledgeService.findknowFileType(Long.parseLong(haveEndId));
					
					haveEndName +=knowFileType.getTypeName()+",";
					
					System.out.println(haveEndId);
				}
				String a = haveEndName.substring(0,haveEndName.length()-1);
				knowledgeManage.setFileDocumentTypeName(a);
			}
			knowledgeManage.setFileDocumentType(haveEnd);
			//保存数据----knowledgeManage对象
			knowledgeService.saveKnowledgeManage(knowledgeManage);
			KnowledgeHandOut knowledgeHandOut = new KnowledgeHandOut();
			knowledgeHandOut.setUserId(Long.parseLong(userId));
			knowledgeHandOut.setKnowledgeId(knowledgeManage.getId());
			knowledgeService.saveKnowledgeHandOut(knowledgeHandOut);
			KnowledgeHandOut knowledgeHandOut1 = new KnowledgeHandOut();
			knowledgeHandOut1.setUserId(2l);
			knowledgeHandOut1.setKnowledgeId(knowledgeManage.getId());
			knowledgeService.saveKnowledgeHandOut(knowledgeHandOut1);
			logService.saveLogInfo(this.getLogBase(), "知识中心", "知识中心管理", "知识中心管理文件添加成功");
			
		}else{
			System.out.println("对象knowledgeManage为空！");
		}
//		logService.saveLogInfo(this.getLogBase(), "文件保存功能成功", "知识中心", "");
		getRequest().setAttribute("message", "保存成功!");
		return "SUCCESS";
		
	}
	/**
	 * 保存进分发表
	 * @author zhn
	 * @date 2013-11-8 上午10:12:53
	 * @return
	 * @throws IOException
	 * @throws ServiceException
	 */
	public void saveFenFaExec() throws IOException, ServiceException{
		setContentType();
		PrintWriter out = PubMethod.GetPrintWriter();
		
		String id = this.getRequest().getParameter("id");
		String userid = this.getRequest().getParameter("userid");
		
		if(userid.indexOf(",")==-1){
			KnowledgeHandOut knowledgeHandOut = new KnowledgeHandOut();
			knowledgeHandOut.setUserId(Long.parseLong(userid));
			knowledgeHandOut.setKnowledgeId(Long.parseLong(id));
			knowledgeService.saveKnowledgeHandOut(knowledgeHandOut);
		}else{
			String[] userids = userid.split(",");
			for (String uid : userids) {
				KnowledgeHandOut knowledgeHandOut = new KnowledgeHandOut();
				knowledgeHandOut.setUserId(Long.parseLong(uid));
				knowledgeHandOut.setKnowledgeId(Long.parseLong(id));
				knowledgeService.saveKnowledgeHandOut(knowledgeHandOut);
			}
			
		}
		out.print("{\"result\":\"success\"}");
	}
	/**
	 * 获得未分发的人员列表
	 * @author zhn
	 * @date 2013-11-9 下午05:39:58
	 * @throws IOException
	 * @throws ServiceException
	 */
	public void searchFenFaUserExec() throws IOException, ServiceException{
		setContentType();
		try {
			Map<String, String> filterMap = getPagerMap();
			//取出页面中的查询条件
			String knowledgeid = getRequest().getParameter("knowledgeid");
			String username = getRequest().getParameter("username");
			String orgId = getRequest().getParameter("orgId");
			List<KnowledgeHandOut> list= knowledgeService.findUserids(knowledgeid);
			String str = "";
			for (int i = 0; i < list.size(); i++) {
				str += list.get(i).getUserId()+",";
			}
			String userids = str.substring(0,str.length()-1);
			
			
			//将查询条件保存到Map里
			if(!StringUtil.isEmpty(knowledgeid))
				filterMap.put("knowledgeid", knowledgeid);
			if(!StringUtil.isEmpty(userids))
				filterMap.put("userids", userids);
			if(!StringUtil.isEmpty(username))
				filterMap.put("username", StringUtil.encodingUrl(username));
			if(!StringUtil.isEmpty(orgId))
				filterMap.put("orgId", orgId);
			String[] viewArray = { "userid", "username","orgNameLabel"};
			// 查询所有的数据
			PageResult result = knowledgeService.searchFenFaUserExec(filterMap);
			String returnStr = getColumnJson(result, viewArray);

			PrintWriter out = getResponse().getWriter();
			out.print(returnStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}
	/**
	 * 功能描述：涉及到文件上传(修改方法)
	 * @return
	 * @throws IOException
	 * @throws ServiceException 
	 */
	public String editFileManageExec() throws IOException, ServiceException{
		setContentType();
		String contentType = this.getRequest().getContentType();
		System.out.println(contentType); 
		if(knowledgeManage!=null)
		{
			
			if(contentType==null || !contentType.startsWith("multipart/form-data"))  //没有文件提交
			{
				knowledgeService.saveKnowledgeManage(knowledgeManage);
			}else{
				List files = getUpload();
				StringBuilder sb = new StringBuilder();
				for (int i = 0 ; i < files.size() ; i++) 
				{ 
					//C:\df\taskfile\knowledgeCenter\故障解决方案\20131211
					String fileAdd = knowledgeManage.getFileAdd();
					 fileAdd = fileAdd.replaceAll("\\\\", "/");   
					String[] treeName = fileAdd.split("/");
					Timestamp date = new Timestamp(System.currentTimeMillis());
					 String Date = new SimpleDateFormat("yyyyMMdd").format(date);
					 String Date1 =Date.substring(0, Date.length());
					String filePath =  (String) this.getRequest().getSession().getAttribute("SYS_FILEPATH");
					String str = filePath.replaceAll("\\\\", "/");   
					String url = str + "/taskfile/knowledgeCenter/"+treeName[4]+"/"+Date1;
					knowledgeManage.setFileAdd(url);
					File file = new File(url);
					if ( ! (file.exists())  &&   ! (file.isDirectory()))  {
		                 boolean  creadok  =  file.mkdirs();
		                 if (creadok)  {
		                    System.out.println( " ok:创建文件夹成功！ " );
		                } else  {
		                    System.out.println( " err:创建文件夹失败！ " );                    
		                } 
		            } 
					String address = url + "/" + getUploadFileName().get(i);
					FileOutputStream fos = new FileOutputStream(address); 
					FileInputStream fis = new FileInputStream(files.get(i).toString()); 
					byte[] buffer = new byte[1024]; 
					int len = 0; 
					while ((len = fis.read(buffer)) > 0) 
					{ 
						fos.write(buffer, 0, len);
					} 
					sb.append(getUploadFileName().get(i));
					knowledgeManage.setFileEntryAddress(address);
				}
			}
			String haveEnd = getRequest().getParameter("haveEnd");
			String haveEndName ="";
			if(!haveEnd.equals("")){
				String[] haveEndList = haveEnd.split(",");
				for (String haveEndId : haveEndList) {
					KnowFileType knowFileType  = knowledgeService.findknowFileType(Long.parseLong(haveEndId));
					
					haveEndName +=knowFileType.getTypeName()+",";
					
					System.out.println(haveEndId);
				}
				String a = haveEndName.substring(0,haveEndName.length()-1);
				knowledgeManage.setFileDocumentTypeName(a);
			}
			
			knowledgeManage.setFileDocumentType(haveEnd);
			//保存数据----knowledgeManage对象
			String userId = getRequest().getSession().getAttribute("TDM_USER_ID").toString();
			String userName = getRequest().getSession().getAttribute("username").toString();
			Timestamp date = new Timestamp(System.currentTimeMillis());
			 String Date = new SimpleDateFormat("yyyy-MM-dd").format(date);
			 String Date1 =Date.substring(0, Date.length());
			knowledgeManage.setSetupUserId(Long.parseLong(userId));
			knowledgeManage.setSetupUserName(userName);
			knowledgeManage.setSetupTime(date);
			knowledgeService.saveKnowledgeManage(knowledgeManage);
			logService.saveLogInfo(this.getLogBase(), "知识中心", "知识中心管理", "知识中心管理文件修改成功");
			
		}else{
			System.out.println("对象knowledgeManage为空！");
		}
//		logService.saveLogInfo(this.getLogBase(), "文件保存功能成功", "知识中心", "");
		getRequest().setAttribute("message", "修改成功!");
		return "SUCCESS";
		
	}
	/**
     * 文件下载
     * @param tdmTestProgram
     * @throws UnsupportedEncodingException
     * @throws ServiceException void
     * @author 张海娜
     */
    public void download() throws UnsupportedEncodingException, ServiceException
	{
    	
    	String FilePath = StringUtil.encodingUrl(getRequest().getParameter("filePath"));
    	String upFileName = FilePath.substring((FilePath.lastIndexOf("/"))+1, FilePath.length());
    	//String upFileName = FilePath.substring((FilePath.lastIndexOf(File.separator))+1, FilePath.length());
		File downloadFile = new File(FilePath);
		
		if(downloadFile.exists())
		{
			getResponse().setHeader("Server","www.tjtt.com");
			getResponse().setHeader("Content-Type", "application/octet-stream");
			getResponse().setHeader("Content-Length", String.valueOf(downloadFile.length()));

			getResponse().setHeader("Content-Disposition", "attachment; filename=\"" + java.net.URLEncoder.encode(upFileName,"UTF-8").replace( "+",   "%20")  + "\"");	
			try {
				OutputStream downloadStream=getResponse().getOutputStream();
				FileInputStream fin=new FileInputStream(downloadFile);
				byte[] b=new byte[1024];				
				int length=fin.read(b);
				while(length>0)
				{
					downloadStream.write(b,0,length);
					length=fin.read(b);
				}
				downloadStream.flush();
				fin.close();	
				logService.saveLogInfo(this.getLogBase(), "知识中心", "知识中心管理", "知识中心管理文件下载成功成功");
			} catch (IOException e) {
				e.printStackTrace();
			}			
		}
		else
		{
			getResponse().setHeader("Content-Type", "text/html; charset=UTF-8");
			try {
				getResponse().getWriter().write("<html>");
				getResponse().getWriter().write("<head>");
				getResponse().getWriter().write("</head>");
				getResponse().getWriter().write("<body>");
				getResponse().getWriter().write("文件没有找到<br>");
				getResponse().getWriter().write("<a href='#' onclick='javascript:window.close()'>关闭</a>");
				getResponse().getWriter().write("</body>");
				getResponse().getWriter().write("</html>");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 查询用户
	 * @throws ServiceException
	 * @throws IOException
	 *//*
	public void queryUsersByRoleId() throws ServiceException, IOException{
			Map filterMap = getPagerMap();  
			String userName ="";
			String deptName ="";
			userName = getRequest().getParameter("userName");
			deptName = getRequest().getParameter("deptName");
			if(userName != null && !"".equals(userName)){
				userName = StringUtil.encodingUrl(userName);
			}
			if(deptName != null && !"".equals(deptName)){
				deptName = StringUtil.encodingUrl(deptName);
			}
			filterMap.put("delFlag","1");
			filterMap.put("userName", userName);
			filterMap.put("deptName", deptName);
			String[] viewArray = {"id","LOGINNAME","USERNAME","org_id","ORG_NAME"};
			//查询所有的数据
			PageResult result =  knowledgeService.queryUsersByRoleId(filterMap);
			String returnStr = getColumnJson(result,viewArray);
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("application/XML,charset=GBK");
			response.setHeader("Charset", "GBK");
			response.setCharacterEncoding("GBK");
			response.setContentType("text/xml");
			PrintWriter out = response.getWriter();
//			logService.saveLogInfo(this.getLogBase(), "查询用户功能成功", "知识中心", "");
			out.print(returnStr);	
	}*/
	/**
	 * 
	 * @throws Exception void
	 * @author 张海娜
	 * @date 2012-10-11 下午04:04:40
	 */
	  public void findKnowledgeByNameMethod() throws Exception{
			PrintWriter out = getResponse().getWriter();
			try {
				String aName = StringUtil.encodingUrl(getRequest().getParameter("typeName"));
				String old =  StringUtil.encodingUrl(getRequest().getParameter("old"));
				if(aName.equals(old)){
					out.print("true");
				}else{
					KnowFileType k = knowledgeService.findKnowledgeByName(StringUtil.escapeString(aName));
					if(k == null){
						out.print("true");
					}else{
						out.print("false");
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				out.print("false");
			}
	}

	public IKnowledgeService getKnowledgeService() {
		return knowledgeService;
	}

	public void setKnowledgeService(IKnowledgeService knowledgeService) {
		this.knowledgeService = knowledgeService;
	}

	public String getTreeId() {
		return treeId;
	}

	public void setTreeId(String treeId) {
		this.treeId = treeId;
	}

	public KnowledgeManage getKnowledgeManage() {
		return knowledgeManage;
	}

	public void setKnowledgeManage(KnowledgeManage knowledgeManage) {
		this.knowledgeManage = knowledgeManage;
	}

	public List getUpload() {
		return upload;
	}

	public void setUpload(List upload) {
		this.upload = upload;
	}

	public List getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(List uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public List getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(List uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getOperator() {
		return operator;
	}

	public void setOperator(String operator) {
		this.operator = operator;
	}

	public int getPid() {
		return pid;
	}

	public void setPid(int pid) {
		this.pid = pid;
	}
	public String getPage() {
		return page;
	}
	public void setPage(String page) {
		this.page = page;
	}
	public String getRows() {
		return rows;
	}
	public void setRows(String rows) {
		this.rows = rows;
	}
	public String getSidx() {
		return sidx;
	}
	public void setSidx(String sidx) {
		this.sidx = sidx;
	}
	public String getSord() {
		return sord;
	}
	public void setSord(String sord) {
		this.sord = sord;
	}
	public String getFileType() {
		return fileType;
	}
	public void setFileType(String fileType) {
		this.fileType = fileType;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public LogService getLogService() {
		return logService;
	}
	public void setLogService(LogService logService) {
		this.logService = logService;
	}
	public EquipmentService getEpService() {
		return epService;
	}
	public void setEpService(EquipmentService epService) {
		this.epService = epService;
	}


//	public LogService getLogService() {
//		return logService;
//	}
//
//	public void setLogService(LogService logService) {
//		this.logService = logService;
//	}

	
}


